package com.macro;

/**
 * @Author macro
 * @Date 2025/6/24  20:15
 * @Description 二分查找[left.right]
 */
public class Solution {
    /**
    *@author macro
    *@description 二分查找
    *@param	nums:有序数组
    *@param	target:查找目标
    *@return {@link int} 返回目标索引
    */
    public static int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(binarySearch(nums, 5));
    }

}